<template>
  <div class="register">
    <div class="title">
      <a class="active" href="/login">登录</a>
      <span>·</span>
      <a href="/register">注册</a>
    </div>
    <div class="Login">
			<label class="LoginName">Register</label>
			<el-form :model="UcenterMember" :rules="rules" ref="UcenterMember" class="demo-ruleForm">

        <el-form-item label="账号" prop="mobile" class="item">
          <input type="text" v-model="UcenterMember.mobile"  />
        </el-form-item>
				<el-form-item label="密码" prop="password" class="item">
				  <input type="password" v-model="UcenterMember.password"  />
				</el-form-item>

				<div class="btn" @click="submitForm" >submit
					<span></span>
					<span></span>
					<span></span>
					<span></span>
				</div>
			</el-form>
		</div>

  </div>
</template>

<script>
  import {login,getMemberInfo} from '~/http'
   import '~/assets/css/sign.css'
   import '~/assets/css/register.css'
   import cookie from "js-cookie"
  export default{
    layout: 'sign',
    data(){
      return {
          loginInfo:{},
          UcenterMember:{
            mobile:"",
            password:""
          },
          rules:{
            mobile:[
               { required: true, message: '请输入手机号', trigger: 'blur' },
               { pattern:/^\d{11}$/, message: '手机号11位', trigger: 'blur' }
            ],
            password:[
                { required: true, message: '请输入密码', trigger: 'blur' },
                { min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur' }
            ]
          }
      }
    },

    created() {

    },
    methods:{
      submitForm(){
          this.$refs.UcenterMember.validate(val=>{
            if(val){
              login(this.UcenterMember).then(res=>{
                let token = res.data.token
                cookie.set("get_token",res.data.token,{domain:"localhost"})
                getMemberInfo().then(res=>{
                  this.loginInfo = res.data.member
                  //cookie.set 参数是 第一个是请求名 第二个是 请求参数 第三个是对象 domain 是请求范围 expires 可以存多长时间
                  cookie.set("get_ucenter",this.loginInfo,{domain:"localhost",expires:7})
                  this.$router.push("/")
                })
              })
            }else{
              return false
            }
          })
      }

    }
  }
</script>
